package com.example.redis.myJredis;

import com.example.redis.JredisUtils;
import redis.clients.jedis.*;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SOAL on 2017/6/6 0006.
 */
public class RedisShardPoolTest {
    static ShardedJedisPool pool;
    static{
        JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
        config.setMaxTotal(500);//最大活动的对象个数
        config.setMaxIdle(1000 * 60);//对象最大空闲时间
        config.setMaxWaitMillis(1000 * 10);//获取对象时最大等待时间
        config.setTestOnBorrow(true);
        String hostA = "127.0.0.1";
        int portA = 6379;
        String hostB = "127.0.0.1";
        int portB = 6380;
        List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>();
        JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
//        infoA.setPassword("redis.360buy");
        JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
//        infoB.setPassword("redis.360buy");
        jdsInfoList.add(infoA);
        jdsInfoList.add(infoB);

        pool =new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH,
                Sharded.DEFAULT_KEY_TAG_PATTERN);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {

        for(int i=0; i<100; i++){
            String key =generateKey();
            //key += "{aaa}";
            ShardedJedis jds =null;
            try {
                jds =pool.getResource();
                System.out.println(key+":"+jds.getShard(key).getClient().getHost());
                jds.set(key,"1111111111111111111111111111111_"+i);
                System.out.println(jds.get(key));

            }catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                pool.returnResource(jds);
            }
        }
    }

    private static int index = 1;
    public static String generateKey(){
        return String.valueOf(Thread.currentThread().getId())+"_"+(index++);
    }
}
